﻿#region

using AspectCore.DynamicProxy.Parameters;

#endregion

namespace Vin.Extension.Aop.Attributes.Parameter;

/// <summary>
/// 方法需要是虚方法（virtual）或者接口方法（interface）才能被拦截
/// </summary>
public class NotNullOrEmptyAttribute(bool canEmpty = false) : VinBaseParameterInterceptorAttribute
{
    
    public bool CanEmpty { get; set; } = canEmpty;

    public override Task Invoke(ParameterAspectContext context, ParameterAspectDelegate next)
    {
        return context.Parameter.Value switch
        {
            null => throw new ArgumentNullException(context.Parameter.Name),
            string str when string.IsNullOrEmpty(str) && !CanEmpty => throw new ArgumentNullException(context.Parameter
                .Name),
            _ => next(context)
        };
    }
}